const EventBus = require("../template")

function testEventBusPerformance() {
  console.log(
    "1. 大量事件订阅性能测试:",
    (() => {
      const eventBus = new EventBus()
      const eventCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < eventCount; i++) {
        eventBus.on(`event${i}`, () => {})
      }
      
      const end = performance.now()
      const subscribeTime = end - start
      
      return subscribeTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "2. 大量事件发布性能测试:",
    (() => {
      const eventBus = new EventBus()
      const eventCount = 1000
      
      // 订阅事件
      for (let i = 0; i < eventCount; i++) {
        eventBus.on(`event${i}`, () => {})
      }
      
      const start = performance.now()
      
      // 发布事件
      for (let i = 0; i < eventCount; i++) {
        eventBus.emit(`event${i}`)
      }
      
      const end = performance.now()
      const emitTime = end - start
      
      return emitTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "3. 大量监听器性能测试:",
    (() => {
      const eventBus = new EventBus()
      const listenerCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < listenerCount; i++) {
        eventBus.on('test', () => {})
      }
      
      const end = performance.now()
      const addTime = end - start
      
      return addTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "4. 大量取消订阅性能测试:",
    (() => {
      const eventBus = new EventBus()
      const listenerCount = 1000
      const callbacks = []
      
      // 添加监听器
      for (let i = 0; i < listenerCount; i++) {
        const callback = () => {}
        callbacks.push(callback)
        eventBus.on('test', callback)
      }
      
      const start = performance.now()
      
      // 取消订阅
      callbacks.forEach(callback => {
        eventBus.off('test', callback)
      })
      
      const end = performance.now()
      const removeTime = end - start
      
      return removeTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "5. 混合操作性能测试:",
    (() => {
      const eventBus = new EventBus()
      const operationCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < operationCount; i++) {
        const operation = Math.floor(Math.random() * 3)
        
        if (operation === 0) {
          eventBus.on(`event${i}`, () => {})
        } else if (operation === 1) {
          eventBus.emit(`event${i}`)
        } else {
          eventBus.off(`event${i}`, () => {})
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "6. 事件总线内存效率测试:",
    (() => {
      const eventBus = new EventBus()
      const eventCount = 1000
      
      // 添加大量事件
      for (let i = 0; i < eventCount; i++) {
        eventBus.on(`event${i}`, () => {})
      }
      
      // 验证内存使用合理
      const eventNames = eventBus.eventNames()
      const totalListeners = eventBus.listenerCount()
      
      return eventNames.length === eventCount &&
             totalListeners === eventCount
    })()
  )

  console.log(
    "7. 事件总线并发操作性能测试:",
    (() => {
      const eventBus = new EventBus()
      const operationCount = 1000
      
      const start = performance.now()
      
      // 并发操作
      for (let i = 0; i < operationCount; i++) {
        eventBus.on(`event${i}`, () => {})
        eventBus.emit(`event${i}`)
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "8. 事件总线批量操作性能测试:",
    (() => {
      const eventBus = new EventBus()
      const batchSize = 100
      const batches = 10
      
      const start = performance.now()
      
      // 批量操作
      for (let batch = 0; batch < batches; batch++) {
        for (let i = 0; i < batchSize; i++) {
          eventBus.on(`batch${batch}`, () => {})
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "9. 事件总线范围查询性能测试:",
    (() => {
      const eventBus = new EventBus()
      const eventCount = 1000
      
      // 添加事件
      for (let i = 0; i < eventCount; i++) {
        eventBus.on(`event${i}`, () => {})
      }
      
      const start = performance.now()
      
      // 范围查询
      const eventNames = eventBus.eventNames()
      
      const end = performance.now()
      const queryTime = end - start
      
      return eventNames.length === eventCount &&
             queryTime < 100
    })()
  )

  console.log(
    "10. 事件总线单点查询性能测试:",
    (() => {
      const eventBus = new EventBus()
      const eventCount = 1000
      
      // 添加事件
      for (let i = 0; i < eventCount; i++) {
        eventBus.on(`event${i}`, () => {})
      }
      
      const start = performance.now()
      
      // 单点查询
      for (let i = 0; i < eventCount; i++) {
        eventBus.listenerCount(`event${i}`)
      }
      
      const end = performance.now()
      const queryTime = end - start
      
      return queryTime < 1000
    })()
  )

  console.log(
    "11. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(count) {
          const eventBus = new EventBus()
          const start = performance.now()
          
          for (let i = 0; i < count; i++) {
            eventBus.on(`event${i}`, () => {})
          }
          
          const end = performance.now()
          const time = end - start
          
          return {
            time: time,
            result: count * this.multiplier
          }
        }
      }
      
      const result = perfObj.processPerformance(100)
      
      return result.result === 200 &&
             result.time < 100
    })()
  )

  console.log(
    "12. 事件总线性能一致性测试:",
    (() => {
      const eventBus1 = new EventBus()
      const eventBus2 = new EventBus()
      const eventCount = 100
      
      // 相同操作测试一致性
      const times1 = []
      const times2 = []
      
      for (let i = 0; i < 10; i++) {
        const start1 = performance.now()
        for (let j = 0; j < eventCount; j++) {
          eventBus1.on(`event${j}`, () => {})
        }
        const end1 = performance.now()
        times1.push(end1 - start1)
        
        const start2 = performance.now()
        for (let j = 0; j < eventCount; j++) {
          eventBus2.on(`event${j}`, () => {})
        }
        const end2 = performance.now()
        times2.push(end2 - start2)
      }
      
      // 验证性能一致性
      const avgTime1 = times1.reduce((a, b) => a + b, 0) / times1.length
      const avgTime2 = times2.reduce((a, b) => a + b, 0) / times2.length
      
      return avgTime1 < 100 &&
             avgTime2 < 100 &&
             Math.abs(avgTime1 - avgTime2) < 50
    })()
  )
}

testEventBusPerformance()
